{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e5c17000",
   "metadata": {},
   "source": [
    "# 本周学习内容\n",
    "\n",
    "## 1.认识[列表](https://docs.python.org/3/tutorial/introduction.html#lists)\n",
    "## 2.列表的取值\n",
    "## 3.列表的一般方法\n",
    "## 4.列表的高级方法\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ad434bb",
   "metadata": {},
   "source": [
    "### 1.1初始列表\n",
    ">1.列表的样式： [元素1,元素2,元素3,...]\n",
    ">2.列表的性质\n",
    ">>1.列表是有顺序的（Sequence Types 序列类型的数据），第一个元素的位置/索引（index）是 0\n",
    ">>2.列表可以存放不同的类型数据， 但是 一般存放相同类型数据\n",
    ">>3.列表可以按照序列数据 进行 取值（index） & 切片（slices）\n",
    "\n",
    "###\n",
    " 列表认识开始、结束和步长值：\n",
    "letters[start:stop:step]\n",
    "\n",
    ">如果没有指定开始值，则默认为0；\n",
    "\n",
    ">如果没有指定结束指，则取列表允许的最大值；\n",
    "\n",
    ">如果没有指定步长值，则默认步长为1.\n",
    "\n",
    " \n",
    "\n",
    ">>首先，列表也是一个变量，构建列表需要用”[]“，里面的数据之间用”,“进行分隔。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "fa17b811",
   "metadata": {},
   "outputs": [],
   "source": [
    "username_database = \"Alex;Jack;zhichao;Marry\"\n",
    "username_database_list = [\"Alex\",\"Jack\",\"zhichao\",\"Marry\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "a59b6c2c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jack\n",
      "Jack\n"
     ]
    }
   ],
   "source": [
    "# 目标取出每一个账号名称\n",
    "print(username_database[5:9]) #字符串就显得比较麻烦\n",
    "print(username_database_list[1]) # 列表可以按照独立元素进行取值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "66f6daa3",
   "metadata": {},
   "outputs": [],
   "source": [
    "#列表中可以包含不同的 types （int\\float\\str..也可是列表）数据 * 不常这样用\n",
    "different_types_list = [\"Alex\",18,175.5,[\"广东省广州市广州南方学院\",510970]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "8dea6c4b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Alex'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "different_types_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "f5bd3882",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "18"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "different_types_list[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "4e38dbe4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['广东省广州市广州南方学院', 510970]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "different_types_list[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "a1e95396",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "510970"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "different_types_list[3][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "2b7ac253",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'jack'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "username_database[5:9]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "b3c133a2",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    },
    {
     "ename": "IndexError",
     "evalue": "list index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-17-aa6ca6c14e8a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0msquares\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m9\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m16\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m25\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msquares\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msquares\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;31m#\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m: list index out of range"
     ]
    }
   ],
   "source": [
    "# 思考:取值是否需要知道列表长度?  如果列表只有5个元素\n",
    "squares = [1,4,9,16,25]\n",
    "print(squares[0])\n",
    "print(squares[10])# IndexError: list index out of range(索引错误): 超过长度\n",
    "# 一般 列表的取值需先知道列表长度,取值不能超过其长度\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db5eb3bd",
   "metadata": {},
   "source": [
    "### 2.1 切片 （slices）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "324fdd47",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "squares"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eecd21b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# (正向取)9,16\n",
    "squares[2:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "f60eaaba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[9, 16]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#反向取\n",
    "squares[-3:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "6e48a0ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[9, 16, 25]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 9,16,25(正向取)\n",
    "squares[2:]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "9a1f970e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[9, 16, 25]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 反向取\n",
    "squares[-3:]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a821435c",
   "metadata": {},
   "source": [
    "##### 结论: 取到末尾或者首位一般不写最好\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "6fb3181f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 9]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#取1和9,回顾切片的步长step [start:stop:step]\n",
    "squares[:3:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "b02090f5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16]"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "squares[:4:1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "955187fc",
   "metadata": {},
   "source": [
    "## 2.2列表取值实践"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "095470d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = \"milliways\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "6eacccda",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "# 请检查word中出现了哪些voewls中的元素,并打印出来\n",
    "#知识点:\n",
    "# 1. for 循环和if 条件判断\n",
    "# 2. in 判断是否存在列表中\n",
    "for i in word:\n",
    "    if i in vowels:\n",
    "        print(i)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "a080b7bd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "i\n"
     ]
    }
   ],
   "source": [
    "# 请检查voewls中出现的word?\n",
    "for i in vowels:\n",
    "# print(i)\n",
    "    if i in word:\n",
    "        print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "0537d99b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入你要查询的文本:uanfuwNEFunwefoefnuefne\n",
      "u\n",
      "a\n",
      "u\n",
      "u\n",
      "e\n",
      "o\n",
      "e\n",
      "u\n",
      "e\n",
      "e\n"
     ]
    }
   ],
   "source": [
    "user_word = input(\"请输入你要查询的文本:\")\n",
    "for i in user_word:\n",
    "    if i in vowels:\n",
    "        print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "609e12f3",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "95217f80",
   "metadata": {},
   "source": [
    "|  方法  | 格式 |   结果   |\n",
    "| :----- | :--: | -------: |\n",
    "| 增 |  append(\"\")  | 增加数据，观察增加的位置。 |\n",
    "| 增 | insert(1,\"Dan\")|insert指定位置增加数据|\n",
    "| 减 |  remove(\"\")  | 指定某个数据进行删除  |\n",
    "| 减 |  pop()  | pop(index=-1, /)默认删除最后一个的值,如果加上指定位置，names.pop(1)<==> del names[1] |\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73e32077",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "79945615",
   "metadata": {},
   "source": [
    "## 3.列表的一般方法\n",
    "> object: 对象,在python中,一切皆是对象\n",
    "\n",
    "> value: (列表中)值,索引 \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12fd05f0",
   "metadata": {},
   "source": [
    "> list增：append(\"  \")增加数据，观察增加的位置。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1749c2de",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "2d03a0d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "names = [\"Mike\",\"Mary\",\"Jan\",\"Jack\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "1355955c",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.append(\"Alex\")\n",
    " #list增：append增加数据，观察增加的位置。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "2c83c26b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Mike', 'Mary', 'Jan', 'Jack', 'Alex']"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "68fdfefa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function append:\n",
      "\n",
      "append(object, /) method of builtins.list instance\n",
      "    Append object to the end of the list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.append)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0dc36d7",
   "metadata": {},
   "source": [
    "> list删：remove(\"  \")  指定某个数据进行删除\n",
    ">> * 注意:remove(\"  \")的一般步骤: 1.先查询 2.再删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "334efdc8",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.remove(\"Mike\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "77d9c2a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Mary', 'Jan', 'Jack', 'Alex']"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "7406af04",
   "metadata": {},
   "outputs": [],
   "source": [
    "num_list = [1,4,4,6,5,4,2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "22b6df43",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "list.remove(x): x not in list",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-114-441f779f73e4>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnum_list\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"4\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m: list.remove(x): x not in list"
     ]
    }
   ],
   "source": [
    "num_list.remove(\"4\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6cabc29",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "a29734b5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.remove)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2022c46a",
   "metadata": {},
   "source": [
    "> 3. pop()    pop(index=-1, /)默认删除最后一个的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "89b6e751",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(index=-1, /) method of builtins.list instance\n",
      "    Remove and return item at index (default last).\n",
      "    \n",
      "    Raises IndexError if list is empty or index is out of range.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "8dacc6d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Alex'"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "804b490c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Mike', 'Mary', 'Jan', 'Jack']"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "61842fe2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Jan'"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names.pop(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "52a66cea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Mike', 'Jack']"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "7a0f03e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 删除实践\n",
    "\n",
    "stu_id_分数 = [78,88,77,78,90,90,78,88,90,89,95]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3e123e38",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "d038d491",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "88\n",
      "90\n",
      "90\n",
      "88\n",
      "90\n",
      "89\n",
      "95\n"
     ]
    }
   ],
   "source": [
    "# remove 用remove时可能需要重复运行  才可能按照指定的要求删除所有的值.\n",
    "# 分数在 低于85分的删除\n",
    "for i in stu_id_分数:\n",
    "    print(i)\n",
    "    if i<85 :\n",
    "        stu_id_分数.remove(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "49c6b4d3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a53a7cf1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "639ce8aa",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25697aa3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd5127c6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5a59ad85",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c1667bbe",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d89fdba3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13f52872",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
